<?php
header("Content-Type: text/html; charset=utf-8");
ini_set("display_startup_errors","1");
ini_set("display_errors","1");
ini_set('max_execution_time','100000');
ini_set('upload_max_filesize', "8M");
ini_set('magic_quotes_gpc','1');
ini_set('register_globals','0');
ini_set('short_open_tag','1');
ini_set('post_max_size','10M');
define("MODULE","module/");
require_once("config.php");
require_once("mysql.php");
	
$PageInput=array(
	'text'=>'', //Голый текст
	'info'=>'', //Информация о странице
	'nrv'=>'', //Видео для страницы
	'img'=>'', //картинка у страницы
	'morftext'=>'', //отформатированая страница
	'key'=>'', //Ключ страницы
	'url'=> ''//ЧПУ ссылка
);

require_once(MODULE."cpu.php");
if($getText) require_once(MODULE."google.php");
if($morfText) require_once(MODULE."morftext.php");
if($infoText) require_once(MODULE."info.php");
if($getVideo) require_once(MODULE."youtobe.php");
if($getImg) require_once(MODULE."imgya.php");

$key=iconv("UTF-8","windows-1251",$_POST['key']);
$MySql->query("SELECT `id` FROM `dor_pages` WHERE `keys`='{$key}'");
$row=$MySql->data();
if($row['id']>0) echo "already";
else  echo connect($key);  

/* $MySql->query("show profiles;");
while($debug_mysql=$MySql->data()) {echo "<BR><BR>"; print_r($debug_mysql); echo "<BR><BR>"; } */
function connect($MyKEY)
{
	global $PageInput,$cpu,$MySql,$minPer,$minCount;
	
	$PageInput['key']=$MyKEY;
	$MySql->collect_query('dor_pages','keys',$PageInput['key']);
	
 	$PageInput['url']=$cpu?rus2eng($PageInput['key']):($_POST['id']+1);
 	$MySql->collect_query('dor_pages','cpu',$PageInput['url']);
	
   	switch(rand(0, 2)){
		case 0: {//ничего не вставляем
			$MySql->collect_query('dor_pages','nrv',0);
			break;
		}
		case 1: {//Вставляем рисунок
			if(function_exists('getFromImgYa')) {
				$PageInput['nrv']=getFromImgYa($PageInput['key'],$PageInput['url']);
				$MySql->collect_query('dor_pages','nrv_html',$PageInput['nrv']);
				$MySql->collect_query('dor_pages','nrv',1);
			}
			else $MySql->collect_query('dor_pages','nrv',0);
			break;
		}
		case 2:{//Вставляем видео ролик
			if(function_exists('getFromYoutobe')) {
				$PageInput['nrv']=getFromYoutobe($PageInput['key']);
				 $MySql->collect_query('dor_pages','nrv',2);
 				 $MySql->collect_query('dor_pages','nrv_html',$PageInput['nrv']); 
			}
			else $MySql->collect_query('dor_pages','nrv',0);
			break;
		}
	}
	
	if(function_exists('getFromGoogle')) {
 		$PageInput['text']=getFromGoogle($PageInput['key']);
		if($PageInput['text']===false) return "captcha";
		else $MySql->collect_query('dor_pages','text',iconv("UTF-8","windows-1251",$PageInput['text']));
	}
	if(function_exists('ModText')){
		$PageInput['morftext']=ModText($PageInput['text'],$PageInput['key'],$PageInput['nrv']); 
 		$MySql->collect_query('dor_pages','html',$PageInput['morftext']); 
		
		if(function_exists('InfoText')) $PageInput['info']=InfoText($PageInput['morftext'],$PageInput['key']); 
	}
	else{
		if(function_exists('InfoText')) $PageInput['info']=InfoText($PageInput['text'],$PageInput['key']); 
	}
	
	$MySql->collect_query('dor_pages','count_word',$PageInput['info']['word']); //слов в тексте
	$MySql->collect_query('dor_pages','count_chars',strlen($PageInput['text'])); //символов в тексте
	$MySql->insert('dor_pages'); //Вставляем страничку в базу
 	$MySql->LastId('dor_pages'); //Берем ID вставленой странички 
 	
   	if((0<count($PageInput['info']['count']) && 0<$MySql->LastId) && ($PageInput['info']['count'][0]['count']>$minCount || $PageInput['info']['count'][0]['per']>$minPer)){
		foreach($PageInput['info']['count'] as $id=>$arr){
			$MySql->collect_query('dor_info','id_page',$MySql->LastId);
			$MySql->collect_query('dor_info','key',iconv("UTF-8","windows-1251",$arr['key']));
			$MySql->collect_query('dor_info','num',$arr['count']);
			$MySql->collect_query('dor_info','per',$arr['per']);
			$MySql->insert('dor_info');
		}
		return "Закончили ключик: ".iconv("windows-1251","UTF-8",$PageInput['key'])."[".($_POST['id']+1)."]<BR>";
	 }
	else {
		$MySql->query("DELETE FROM `dor_pages` WHERE `id`='{$MySql->LastId}'");
		return "Неподходящий текст для ключика: ".iconv("windows-1251","UTF-8",$PageInput['key'])."[".($_POST['id']+1)."]<BR>";
	}
}
?>
